﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

namespace lcs.Web.supply
{
    public partial class ListSupplyDate : System.Web.UI.Page
    {
        Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();
        Maticsoft.BLL.monthSupplayPlan msm = new Maticsoft.BLL.monthSupplayPlan();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }
                //Bind(11);
            }
        }

        private void Bind(int user_id)
        {
            string str = "  user_id= " + user_id + " and innums < num  and technicalDataStatus ='已确认' ";//已入库数量 < 计划数量或者已入库数量为null时
            //and supplyDateStatus is null

            if (!string.IsNullOrEmpty(txt_material.Text))
            {
                str += " and material like '%" + txt_material.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(txt_projectName.Text))
            {
                str += " and projectName like '%" + txt_projectName.Text + "%' ";
            }

            if (!string.IsNullOrEmpty(txt_supplier.Text))
            {
                str += " and sname like '%" + txt_supplier.Text + "%' ";
            }
            if (!string.IsNullOrEmpty(txt_purchaseid.Text))
            {
                str += " and purchaseid like '%" + txt_purchaseid.Text + "%' ";
            }

            str += " and supplyDateStatus ='" + this.ddl_ghqzt.SelectedValue + "' ";

            

            DataSet ds = sm.GetListSumInnumsInfo(str);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_add")//供货期确认
            {
                if (e.CommandArgument != null)
                {
                    //string supplyPlanId = e.CommandArgument.ToString();

                    //Response.Redirect("AddMonthSupply.aspx?supplyPlanId="+supplyPlanId);

                    Session["supplyPlanId"] = e.CommandArgument.ToString();

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FramePopSupplyDate.aspx','900px','390px');</script>");
                }

              
            }
            if (e.CommandName == "outconfirm")//确认函
            {
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl, supplier;//需用单位，项目名称，采购订单号，合同交货期，招标批次，货物名称,供应商名称
                //int pid = int.Parse(gwSearch.DataKeys[gvr.RowIndex].Value.ToString());    
                jhnum = gvr.Cells[15].Text.Trim();//中标批次
                wl = gvr.Cells[7].Text.Trim();//物料描述
                id = gvr.Cells[16].Text.Trim();//采购订单号
                //unit = gvr.Cells[11].Text.Trim();
                unit = gvr.Cells[3].Text.Trim();//主送和供应商回复中经办人都改为了供应商名称
                jdate = gvr.Cells[4].Text.Trim();//供货期
                proname = gvr.Cells[5].Text.Trim();//项目名称
                supplier = gvr.Cells[3].Text.Trim();//供应商名称
                //name = ((Label)gvr.Cells[25].Controls[1]).Text;
                //fax = ((Label)gvr.Cells[26].Controls[1]).Text;
                string file = ExWord.ExportToWordconfirm(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
            if (e.CommandName == "outex")//催交函
            {
                //int index = int.Parse(e.CommandArgument.ToString());
                GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
                string unit, proname, id, jdate, jhnum, wl, supplier;
                jhnum = gvr.Cells[15].Text.Trim();//中标批次
                wl = gvr.Cells[7].Text.Trim();//物料描述
                id = gvr.Cells[16].Text.Trim();//采购订单号
                //unit = gvr.Cells[11].Text.Trim();
                unit = gvr.Cells[3].Text.Trim();//主送和供应商回复中经办人都改为了供应商名称
                jdate = gvr.Cells[4].Text.Trim();//供货期
                proname = gvr.Cells[5].Text.Trim();//项目名称
                supplier = gvr.Cells[3].Text.Trim();//供应商名称
                string file = ExWord.ExportToWordExped(unit, jdate, jhnum, proname, id, wl);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();

            }
            if (e.CommandName == "upSupplyDate")//修改供货期
            {
                if (e.CommandArgument != null)
                {
                    Session["updateSupplyDateId"] = e.CommandArgument.ToString();

                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>OpenPop('FramePopUpdateSupplyDate.aspx','500px','290px');</script>");
                }
            }
            if (e.CommandName == "lbn_early")//提前到货
            {
                if (e.CommandArgument != null)
                {
                    int supplyPlanId = int.Parse(e.CommandArgument.ToString());

                    Maticsoft.Model.supplyPlan s = sm.GetModel(supplyPlanId);

                    s.isEarly = true;

                    if (sm.Update(s))
                    {
                        Maticsoft.Common.MessageBox.Show(this,"设置提前到货成功！");
                    }
                }
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

        }

        protected void btn_reload_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }
        }

        protected void btn_confirm_Click(object sender, EventArgs e)
        {
            string supplyDate = txt_supplyDate.Text;//供货期

            if (!string.IsNullOrEmpty(supplyDate))
            {
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                    if (cbo != null && cbo.Checked)
                    {
                        Label lbl = this.GridView1.Rows[i].FindControl("lbl_supplyPlanId") as Label;

                        if (!string.IsNullOrEmpty(lbl.Text))
                        {
                            string supplyPlanId = lbl.Text;

                            Maticsoft.Model.supplyPlan s = sm.GetModel(int.Parse(supplyPlanId));

                            s.supplyDateStatus = "已确认";
                            s.supplyDateStatusDate = DateTime.Now;
                            s.supplyDate = DateTime.Parse(supplyDate);

                            sm.Update(s);
                        }
                    }

                }

                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请确认日期！");
            }
        }

      
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_supplyDateStatus = e.Row.FindControl("lbl_supplyDateStatus") as Label;
                Label lbl_supplyPlanId = e.Row.FindControl("lbl_supplyPlanId") as Label;

                LinkButton lbn_add = e.Row.FindControl("lbn_add") as LinkButton;//供货期确认按钮

                if (lbl_supplyDateStatus.Text.Trim() == "已确认")
                {
                    lbn_add.ForeColor = System.Drawing.Color.Red;
                }

                DataSet ds_mcount = msm.GetList(" supplyPlanId = "+lbl_supplyPlanId.Text);

                if (ds_mcount.Tables[0].Rows.Count > 0)//如果存在月供应计划
                {
                    lbn_add.Enabled = false;
                }
                else
                {
                    lbn_add.Enabled = true;
                }

                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                        e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                }

            }

            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }
        //更改备注
        protected void btn_upReark_Click(object sender, EventArgs e)
        {
            bool flag = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                if (cbo != null && cbo.Checked)
                {
                    flag = true;
                }
            }

            if (flag)
            {
                Maticsoft.BLL.supplyPlan sm = new Maticsoft.BLL.supplyPlan();
                string remark = this.txt_remark.Text;
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                    if (cbo != null && cbo.Checked)
                    {
                        Label lbl = this.GridView1.Rows[i].FindControl("lbl_supplyPlanId") as Label;

                        if (!string.IsNullOrEmpty(lbl.Text))
                        {
                            string supplyPlanId = lbl.Text;

                            Maticsoft.Model.supplyPlan s = sm.GetModel(int.Parse(supplyPlanId));

                            s.remark = remark;

                            sm.Update(s);
                        }
                    }
                }

                if (Session["login"] != null)
                {
                    this.txt_remark.Text = "";

                    DataSet ds = Session["login"] as DataSet;

                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    Bind(user_id);
                }

            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "请选择要修改的备注提醒！");
            }
        }
        //导出
        protected void btn_outexcel_Click(object sender, EventArgs e)
        {
            //this.GridView1.AllowPaging = false;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                Bind(user_id);
            }

            this.GridView1.Columns[0].Visible = false;
            this.GridView1.Columns[1].Visible = false;

            if (this.GridView1.Rows.Count != 0)
            {
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=yuegongjihua.xls");
                //Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                Response.Output.Write("<H4 align='center'>月供应计划</H4>");
                this.GridView1.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();

            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('数据为空，无法导出表格')</script>");

            }
        }

        //导出复写
        public override void VerifyRenderingInServerForm(Control control)       //导出
        { }
        

    

    }
}